package com.cssw.fyzb.busi.api.system;

import com.cssw.fyzb.busi.service.system.AppVersionService;
import com.cssw.fyzb.core.model.dodb.AppVersion;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.dbframe.kit.StrKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author zhouxu
 * @create 2020-08-25 17:29
 */

@Function(description = "查询最新版本信息", parameters = {
        @Parameter(name = "appName", type = Type.String, description = "APP名称", required = true),
        @Parameter(name = "versionCode", type = Type.String, description = "大版本", required = false),
        @Parameter(name = "versionName", type = Type.String, description = "小版本", required = false),
        @Parameter(name = "type", type = Type.String, description = "平台（android、ios）", required = false),

})
@Component
public class GetAppVersion implements Action {

    private static final Logger logger = LoggerFactory.getLogger(GetAppVersion.class);

    @Autowired
    private AppVersionService appVersionService;

    @Override
    public Response execute(Request request) throws Exception {
        String appName = request.getString("appName");
        String versionName = request.getString("versionName");
        String versionCode = request.getString("versionCode");
        String type = request.getString("type");
        logger.debug("appName:" + appName + ",versionCode:" + versionCode + ",versionCode:" + versionCode + ",type:" + type);

        if (StrKit.isBlank(type)) {
            type = "android";
        }

        AppVersion appVersion = appVersionService.getVersion(appName, type);
        if (appVersion == null) {
            return Response.fail("未找到版本信息：appName = " + appName + " ,type = " + type);
        }
        return Response.success().put("data", appVersion);

    }

}
